﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YJYpublicClass;
using YJYpublicClass.DataStruct;

namespace WindowsFormsApp2.PLC
{
    public partial class Frm_PLC设置 : Form
    {
        ModbusTcp_PLC tempPLC = null;
        public Frm_PLC设置(ModbusTcp_PLC obj)
        {
            InitializeComponent();
            tempPLC = obj;
        }
        private void Frm_PLC设置_Load(object sender, EventArgs e)
        {
            this.pdv_PLC参数.SelectedObject = tempPLC.plcconfig;
            timer1.Enabled = true;
        }
        private void btn_PLC连接_Click(object sender, EventArgs e)
        {
            tempPLC.ConnectSrver(tempPLC.plcconfig.Ipaddress,tempPLC.plcconfig.port.ToString());
        }
    
        private void btn_断开连接_Click(object sender, EventArgs e)
        {
            tempPLC.DisConnect();
        }

        private void btn_线圈写入_Click(object sender, EventArgs e)
        {
            Boolean value = true;
            if (string.Equals(this.txb_写入测试值.Text, "true", StringComparison.InvariantCultureIgnoreCase))
            {
                value = true;
            }
            else if (string.Equals(this.txb_写入测试值.Text, "1", StringComparison.InvariantCultureIgnoreCase))
            {
                value = true;
            }
            else
            {
                value = false;
            }
            if (tempPLC.WriteCoil(this.txb_写入测试地址.Text, value))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_Ushort写入_Click(object sender, EventArgs e)
        {
            var value = Convert.ToUInt16(this.txb_写入测试值.Text);
            if (tempPLC.WriteD(this.txb_写入测试地址.Text, value))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_float写入_Click(object sender, EventArgs e)
        {
            var value = Convert.ToSingle(this.txb_写入测试值.Text);

            if (tempPLC.WriteF(this.txb_写入测试地址.Text, value))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_double写入_Click(object sender, EventArgs e)
        {

            if (tempPLC.WriteDouble(this.txb_写入测试地址.Text, this.txb_写入测试值.Text))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_线圈读取_Click(object sender, EventArgs e)
        {
         
            if(tempPLC.ReadCoil(this.txb_读取测试地址.Text, out Boolean value))
            {
                richTextBox1.AppendText(value + "\r\n");
                this.txt_读取结果值.Text = value.ToString();
            }
            else
            {
                richTextBox1.AppendText("读取失败" + "\r\n");
            }
          
        }

        private void btn_ushort读取_Click(object sender, EventArgs e)
        {
            int value = 0;
            tempPLC.ReadD(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value.ToString();
        }

        private void btn_float读取_Click(object sender, EventArgs e)
        {
            
            if(tempPLC.ReadF(this.txb_读取测试地址.Text, out float value))
            {
                richTextBox1.AppendText(value + "\r\n");
                this.txt_读取结果值.Text = value.ToString();
            }else
            {
                richTextBox1.AppendText("读取失败" + "\r\n");
            }
           
        }

        private void btn_double读取_Click(object sender, EventArgs e)
        {
            string value = "";
            tempPLC.ReadDouble(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value;
        }

        private void btn_保存并更新_Click(object sender, EventArgs e)
        {
            if (tempPLC.Saveconfig().State==Funstate.OK)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(tempPLC.IsConnect)
            {
                cbx_连接状态.Checked = true;
            }else
            {
                cbx_连接状态.Checked = false;
            }
        }
    }
}
